package com.alen.sun.service.Impl;

import com.alen.sun.bean.Admin;
import com.alen.sun.bean.Order;
import com.alen.sun.bean.Package;
import com.alen.sun.bean.PageBean;
import com.alen.sun.dao.ElderlyMapper;
import com.alen.sun.dao.OrderMapper;
import com.alen.sun.dao.PackageMapper;
import com.alen.sun.service.OrderService;
import com.alen.sun.util.PhoneCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private PackageMapper packageMapper;
    @Autowired
    private ElderlyMapper elderlyMapper;

    private PageBean pageBean;

    /**
     * 添加订单
     * @param order
     * @return
     */
    @Override
    public Boolean addOrder(Order order) {
        //判断提交数数据不为空
        if(order.getPackageNum()!=null && order.getPackageNum()!=0
            && order.getPackagePrice()!=null && order.getPackagePrice()!=0.0
            && order.getElderlyId()!=null && !order.getElderlyId().isEmpty()
            && order.getPackageId()!=null && !order.getPackageId().isEmpty()){
            //先判断老人id是否在系统中
            Boolean elderlyFlag = elderlyMapper.selectByElderlyId(order.getElderlyId());
            if(!elderlyFlag){
                //数据库中没有这个老人id,就退出生成订单操作
                return false;
            }
            //生成id
            PhoneCode phoneCode = new PhoneCode();
            while (true){
                String orderId = phoneCode.generateCode();
                //查询id是否重复
                Integer count = orderMapper.selectById(orderId);
                if(count == null || count ==0){
                    //没有重复的可以用
                    order.setOrderId(orderId);
                    //跳出循环
                    break;
                }
            }
            //获得统一的当前时间
            LocalDateTime getTime = LocalDateTime.now();
            order.setOrderTime(getTime);
            order.setPackageStart(getTime);
            order.setOrderCancelTime(getTime.plusMinutes(30));
            order.setOrderStatus(0);

            // 计算订单结束时间
            LocalDateTime packageEnd = order.getPackageStart().plusDays(order.getPackageNum() * 31);
            order.setPackageEnd(packageEnd);
            //查询packageId对应的数据
            Package qryPackage = packageMapper.selectByPackageId(order.getPackageId());
            if(qryPackage==null){
                log.info("判定出没有这个套餐id"+qryPackage);
                return false;
            }
            //套餐价格
            order.setPackagePrice(qryPackage.getPackagePrice());
            //订单总价格
            order.setTotalAmount(order.getPackageNum()*qryPackage.getPackagePrice());
            //添加套餐名称
            order.setPackageName(qryPackage.getPackageName());
            //执行添加操作
            Boolean flag = orderMapper.addOrder(order);
            return flag;
        }
        return false;
    }

    /**
     * 修改订单状态
     * @param order
     * @return
     */
    @Override
    public Boolean updateOrder(Order order) {
        //判断提交数数据不为空
        if(order.getOrderId()!=null && !order.getOrderId().isEmpty()
                && order.getOrderStatus()!=null){
            Boolean flag = orderMapper.updateOrder(order);
            return flag;
        }
        return false;
    }

    /**
     * 删除订单
     * @param order
     * @return
     */
    @Override
    public Boolean deleteOrder(Order order) {
        Boolean flag = orderMapper.deleteOrder(order);
        return flag;
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1. 设置分页参数
        //计算参数start
        Integer start = (page-1)*pageSize;
        List<Order> orderList = orderMapper.page(start, pageSize);
        //2.查询总数
        Long count = orderMapper.count();
        pageBean = new PageBean(count,orderList);
        return pageBean;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public Boolean deleteByIds(List<String> ids) {
        Boolean flag = orderMapper.deleteByIds(ids);
        return flag;
    }

    /**
     * 非基础功能
     * @param elderlyId
     * @return
     */

    //家属端,全部订单
    @Override
    public PageBean getOrderByElderlyIdAll(String elderlyId,Integer page, Integer pageSize) {
        Integer start = page*pageSize;
        List<Order> orders = orderMapper.getOrderByElderlyIdAll(elderlyId,start,pageSize);
        //2.查询总数
        Long count = orderMapper.countAll(elderlyId);
        pageBean = new PageBean(count,orders);
        return pageBean;
    }

    //家属端,未支付订单
    @Override
    public PageBean getOrderByElderlyIdUnpaid(String elderlyId,Integer page, Integer pageSize) {
        Integer start = page*pageSize;
        List<Order> orders = orderMapper.getOrderByElderlyIdUnpaid(elderlyId,start,pageSize);
        //2.查询总数
        Long count = orderMapper.countUnpaid(elderlyId);
        pageBean = new PageBean(count,orders);
        return pageBean;
    }

    //家属端,已支付订单
    @Override
    public PageBean getOrderByElderlyIdPaid(String elderlyId,Integer page, Integer pageSize) {
        Integer start = page*pageSize;
        List<Order> orders = orderMapper.getOrderByElderlyIdPaid(elderlyId,start,pageSize);
        //2.查询总数
        Long count = orderMapper.countPaid(elderlyId);
        pageBean = new PageBean(count,orders);
        return pageBean;
    }

    /**
     * 30分钟后取消未支付的订单
     * @return
     */
    @Override
    public Boolean cancelUnpaidOrders() {
        //sql查询未支付状态的订单,条件,当前时间和订单取消时间对比,获取到超时订单,修改订单状态
        List<Order> orders = orderMapper.unpaidOrders();
        //循环修改订单的状态
        List<String> orderIdList = new ArrayList<>();
        for (Order order : orders) {
            orderIdList.add(order.getOrderId());
        }

        if(orderIdList.size()!=0){
            //进行mapper
            Boolean flag = orderMapper.updateByIds(orderIdList);
            return flag;
        }
        return true;
    }

    /**
     * 网页搜索功能,聚合模糊查询,订单编号,套餐名
     * @param keywords
     * @return
     */
    @Override
    public List<Order> searchByKeywords(String keywords) {
        List<Order> orders = orderMapper.searchByKeywords(keywords);
        return orders;
    }

    @Override
    public List<Map<String, Object>> getOrderTotalAmountAndTime() {
        List<Map<String, Object>> resultList = orderMapper.getOrderTotalAmountAndTime();
        return resultList;
    }

    @Override
    public List<Map<String, Object>> getDaysUntil() {
        List<Map<String, Object>> daysUntil = orderMapper.getDaysUntil();
        return daysUntil;
    }
}
